package com.atguigu.tingshu.live.api;/*
 * title:
 * author:
 * date:
 * description:
 * */

import com.alibaba.fastjson.JSON;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.live.config.ChatRoomContainer;
import com.atguigu.tingshu.model.live.FromUser;
import com.atguigu.tingshu.model.live.SocketMsg;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import jakarta.annotation.Resource;
import jakarta.websocket.*;
import jakarta.websocket.server.PathParam;
import jakarta.websocket.server.ServerEndpoint;
import org.joda.time.LocalDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;

@Controller
@ServerEndpoint("/api/websocket/{liveRoomId}/{token}")
public class WebSocketController {
//    @Autowired
    private static RedisTemplate redisTemplate;

    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        WebSocketController.redisTemplate = redisTemplate;
    }

    @OnOpen
    public void onOpen(Session session, @PathParam("liveRoomId") Long liveRoomId, @PathParam("token")String token ){
         System.out.println("建立连接"+liveRoomId+"===="+token);
         UserInfoVo userInfoVo = (UserInfoVo)redisTemplate.opsForValue().get(RedisConstant.USER_LOGIN_KEY_PREFIX + token);
         ChatRoomContainer.addUser(liveRoomId, userInfoVo.getId(), session);
         SocketMsg msg = new SocketMsg();
         msg.setMsgContent(userInfoVo.getNickname() + "加入了直播间");
         msg.setMsgType(SocketMsg.MsgTypeEnum.JOIN_CHAT.getCode());
         msg.setLiveRoomId(liveRoomId);
         msg.setTime(LocalDateTime.now().toString("yyyy-MM-dd HH:mm:ss"));

         FromUser fromUser = new FromUser();
         fromUser.setUserId(userInfoVo.getId());
         fromUser.setNickname(userInfoVo.getNickname());
         fromUser.setAvatarUrl(userInfoVo.getAvatarUrl());
         msg.setFromUser(fromUser);
//         ChatRoomContainer.sendMsg(JSON.toJSONString(msg));
        redisTemplate.convertAndSend(RedisConstant.LIVE_CHAT_CHANNEL, JSON.toJSONString(msg));
     }
    @OnClose
    public void onClose(Session session, @PathParam("liveRoomId") Long liveRoomId, @PathParam("token")String token ){
        System.out.println("关闭连接"+liveRoomId+"===="+token);
        UserInfoVo userInfoVo = (UserInfoVo)redisTemplate.opsForValue().get(RedisConstant.USER_LOGIN_KEY_PREFIX + token);


        ChatRoomContainer.removeUser(liveRoomId, userInfoVo.getId());

        SocketMsg msg = new SocketMsg();
        msg.setMsgContent(userInfoVo.getNickname() + "离开了直播间");
        msg.setMsgType(SocketMsg.MsgTypeEnum.CLOSE_SOCKET.getCode());
        msg.setLiveRoomId(liveRoomId);
        msg.setTime(LocalDateTime.now().toString("yyyy-MM-dd HH:mm:ss"));

        FromUser fromUser = new FromUser();
        fromUser.setUserId(userInfoVo.getId());
        fromUser.setNickname(userInfoVo.getNickname());
        fromUser.setAvatarUrl(userInfoVo.getAvatarUrl());
        msg.setFromUser(fromUser);
//        ChatRoomContainer.sendMsg(JSON.toJSONString(msg));
        redisTemplate.convertAndSend(RedisConstant.LIVE_CHAT_CHANNEL, JSON.toJSONString(msg));
    }
    @OnMessage
    public void OnMessage(Session session,String msg){
        System.out.println("获取了消息："+msg);
//        ChatRoomContainer.sendMsg(msg);
        redisTemplate.convertAndSend(RedisConstant.LIVE_CHAT_CHANNEL, msg);
    }
    @OnError
    public void OnError(Session session,Throwable ex){
        System.out.println("连接出错："+ex.getMessage());
    }
}
